package com.roads.project.service.impl;

import com.roads.common.utils.StringUtils;
import com.roads.project.domain.ProInstitutionUser;
import com.roads.project.mapper.ProInstitutionUserMapper;
import com.roads.project.service.IProInstitutionUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ProInstitutionUserServiceImpl
 * @Description 机构用户service实现类
 * @Author nhp
 * @Date 2021/11/19 0019 16:18
 * @Version 1.0.0
 **/
@Service
public class ProInstitutionUserServiceImpl implements IProInstitutionUserService {

    @Autowired
    private ProInstitutionUserMapper institutionUserMapper;

    @Override
    public List<ProInstitutionUser> selectUserByCode(String code) {
        return institutionUserMapper.selectUserByCode(code);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveInstitutionUser(String innerCode, String users) {
        // 删除用户
        institutionUserMapper.deleteByCode(innerCode);
        if (StringUtils.isNotEmpty(users)) {
            // 转集合，批量插入
            List<ProInstitutionUser> list = new ArrayList<>();
            String[] userArray = users.split(",");
            if (userArray.length >0) {
                for (int i = 0; i < userArray.length; i++) {
                    ProInstitutionUser user = new ProInstitutionUser();
                    user.setInstitutionCode(innerCode);
                    user.setUserName(userArray[i]);
                    list.add(user);
                }
                institutionUserMapper.batchInsert(list);
            }
        }
        return 1;
    }
}
